﻿using ParkSaleForAdmin.Data;
using ParkSaleForAdmin.Foundation;
using System.Windows;

namespace ParkSaleForAdmin.FunctionWindow
{
    public class MessageInfo
    {
        [DataToUI("消息内容")]
        public string content
        {
            get;
            set;
        }
        [DataToUI("消息类型")]
        public string type
        {
            get;
            set;
        }
        [DataToUI("收件人")]
        public string to
        {
            get;
            set;
        }
        [DataToUI("发件人")]
        public string from
        {
            get;
            set;
        }
        [DataToUI("消息编号", false)]
        public string id
        {
            get;
            set;
        }
        [DataToUI("发送时间", false)]
        public string time
        {
            get;
            set;
        }

        [DataToUI("处理状态 0未处理 1已处理(录入时无用)")]
        public string handled
        {
            get;
            set;
        }
        [DataToUI("消息参数")]
        public string arg
        {
            get;
            set;
        }
    }

    class MessageManager : BasicManagerWindow<MessageInfo>
    {
        public override string TableName => "Messages";

        protected override void add_Click(object sender, RoutedEventArgs e)
        {
            nullable = 0;
            string code = "", text = "", send = "", geter = "", args = "";
            foreach (var i in CollectData())
            {
                if (i.Item1.Equals("type"))
                {
                    code = i.Item2;
                }
                if (i.Item1.Equals("content"))
                {
                    text = i.Item2;
                }
                if (i.Item1.Equals("from"))
                {
                    send = i.Item2;
                }
                if (i.Item1.Equals("to"))
                {
                    geter = i.Item2;
                }
                if (i.Item1.Equals("arg"))
                {
                    args = i.Item2;
                }
            }

            SystemManager.Request<int>(SuUserRequestInfo.s_MessageSend, delegate (object obj)
            {
                if (obj != null)
                {
                    var r = (int)obj;
                    if (r == SuUserResponseInfo<int>.OK)
                    {
                        MessageBox.Show("发送成功");

                    }
                    else
                    {
                        MessageBox.Show($"发送失败，Code:{r}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                else
                {
                    MessageBox.Show("发送失败，网络错误", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }



            }, null, code, text, send, geter, args);

            //base.add_Click(sender, e);
        }
        int nullable = 0;
        protected override bool NullablePending(string value)
        {
            if (value.Equals("handled") || value.Equals("arg"))
            {
                nullable++;
                return true;
            }
            return false;
        }


        public override string AppendStr(MessageInfo obj)
        {
            return SQLHelpler.MakeSelectStr(TableName, "id", 9999999999.ToString());
        }

        public override string ChangeStr(MessageInfo obj)
        {
            return SQLHelpler.MakeSelectStr(TableName, "id", obj.id);
        }

        public override string DeleteStr(MessageInfo obj)
        {
            return SQLHelpler.MakeSelectStr(TableName, "id", obj.id);
        }
    }
}
